#!/bin/bash
#set -x
#set -e

source ./config/config
source ./script/lib.sh

# 查看es健康
# kubectl exec -it
# curl -s http://efk-elasticsearch-coordinating-only.cloud-infra:9200/_cat/health?v
# curl -s http://${efk_svc}/_cat/health?v -u elastic -pm12345 >> ${file_output}


echo -e "\033[93m|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++|"
echo -e "|                        elasticsearch                     |"
echo -e "|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++|\033[0m"

ELASTICSEARCH_URL=""
if [ -z ${EFK_ACCOUNT} ];then
    ELASTICSEARCH_URL="http://${ELASTICSEARCH_HOST}:9200"
else
    ELASTICSEARCH_URL="http://${EFK_ACCOUNT}:${EFK_PASSWORD}@${ELASTICSEARCH_HOST}:9200"
fi

echo -e "#################  elasticsearch健康状态  #################"
#es_health_exception=`curl -s http://elastic:m12345@192.168.220.91:30518/_cat/health?v`  # 虚拟机部署时测试用
es_health_exception=`curl -s ${ELASTICSEARCH_URL}/_cat/health?v`
es_health_exception_result=`curl -s ${ELASTICSEARCH_URL}/_cat/health?v |  awk '{print $4}' |tail -n 1`

if [[ $es_health_exception_result = "red" ]];then
	wResult "FATAL" es_health_exception
elif [[ $es_health_exception_result = "yellow" ]];then
	wResult "WARNING" es_health_exception
else
	wResult "NORMAL" es_health_exception
fi

wDetail "$es_health_exception" es_health_exception

echo -e "#################  elasticsearch节点列表  #################"
#es_node_status=`curl -s http://elastic:m12345@192.168.220.91:30518/_cat/nodes?v`  # 虚拟机部署时测试用

es_node_status=`curl -s ${ELASTICSEARCH_URL}/_cat/nodes?v`
es_node_status=`curl -s ${ELASTICSEARCH_URL}/_cat/nodes?v`

wDetail "$es_node_status" es_node_status

echo -e "#################  elasticsearch索引状态  #################"
#es_index_info=`curl -s http://elastic:m12345@192.168.220.91:30518/_cat/indices?v`  # 虚拟机部署时测试用

es_index_info=`curl -s ${ELASTICSEARCH_URL}/_cat/indices?v`
es_index_info_fatal=`curl -s ${ELASTICSEARCH_URL}/_cat/indices?v | grep red`
es_index_info_warning=`curl -s ${ELASTICSEARCH_URL}/_cat/indices?v | grep yellow`

if [[ -n $es_index_info_fatal ]];then
	wResult "FATAL" es_index_info
elif [[ -n $es_index_info_warning ]];then
	wResult "WARNING" es_index_info
else
	wResult "NORMAL" es_index_info
fi
wDetail "$es_index_info" es_index_info

echo -e "#################  elasticsearch安全认证  #################"

es_security_info=`curl -s ${ELASTICSEARCH_URL}/_cat/indices?v | grep .security-7`

if [[ -n $es_security_info ]];then
	wResult "NORMAL" es_security_info
	wDetail "有安全认证" es_security_info
else
	wResult "WARNING" es_security_info
	wDetail "没有安全认证" es_security_info
fi

echo -e "#################  elasticsearch分片查询  #################"
#es_shard_info=`curl -s http://elastic:m12345@192.168.220.91:30518/_cat/shards?v`  # 虚拟机部署时测试用

es_shard_info=`curl -s ${ELASTICSEARCH_URL}/_cat/shards?v`
es_shard_info_fatal=`curl -s ${ELASTICSEARCH_URL}/_cat/shards?v | grep UNASSIGNED`

if [[ -n $es_shard_info_fatal ]];then
	wResult "FATAL" es_shard_info
else
	wResult "NORMAL" es_shard_info
fi
wDetail "$es_shard_info" es_shard_info

echo -e "#################  elasticsearch集群设置  #################"
#es_setting_info=`curl -s http://elastic:m12345@192.168.220.91:30518/_cluster/settings`  # 虚拟机部署时测试用
es_setting_info=`curl -s ${ELASTICSEARCH_URL}/_cluster/settings`
wDetail "$es_setting_info" es_setting_info